#!/usr/bin/env bash
# Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

# Script to run a benchmark compiled using dart2wasm. Assumes the Dart repo's
# directory structure.

set -e

function follow_links() {
  file="$1"
  while [ -h "$file" ]; do
    # On Mac OS, readlink -f doesn't work.
    file="$(readlink "$file")"
  done
  echo "$file"
}

# Unlike $0, $BASH_SOURCE points to the absolute path of this file.
PROG_NAME="$(follow_links "$BASH_SOURCE")"

# Handle the case where dart-sdk/bin has been symlinked to.
PROG_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
SDK_DIR="$(cd "${PROG_DIR}/../../.." ; pwd -P)"

# Hardcoded to x64 Linux for now.
D8="$SDK_DIR/third_party/d8/linux/x64/d8"

RUN_WASM="$SDK_DIR/pkg/dart2wasm/bin/run_wasm.js"

# We locate the JS runtime based on the input wasm file.
exec "$D8" "$RUN_WASM" -- "$(realpath -- "${1%.*}.mjs")" "$1" -- "${@:2}"
